package com.yugao.fintech.draper.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yugao.fintech.draper.admin.convert.DictConvert;
import com.yugao.fintech.draper.admin.convert.DictItemConvert;
import com.yugao.fintech.draper.admin.entity.SysDict;
import com.yugao.fintech.draper.admin.entity.SysDictItem;
import com.yugao.fintech.draper.admin.mapper.DictItemMapper;
import com.yugao.fintech.draper.admin.mapper.DictMapper;
import com.yugao.fintech.draper.admin.param.DictDTO;
import com.yugao.fintech.draper.admin.param.DictItemDTO;
import com.yugao.fintech.draper.admin.param.DictQuery;
import com.yugao.fintech.draper.admin.service.DictService;
import com.yugao.fintech.draper.core.enums.DictTypeEnum;
import com.yugao.fintech.draper.core.exception.BaseException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictItemMapper dictItemMapper;

    @Override
    public IPage<DictDTO> pageQuery(DictQuery dictQuery) {
        LambdaQueryWrapper<SysDict> wrapper = Wrappers.<SysDict>lambdaQuery()
                .like(StrUtil.isNotBlank(dictQuery.getType()), SysDict::getType, dictQuery.getType())
                .eq(StrUtil.isNotBlank(dictQuery.getSystemFlag()), SysDict::getSystemFlag,
                        dictQuery.getSystemFlag());
        Page<SysDict> pageInfo = dictMapper.selectPage(Page.of(dictQuery.getPageNum(), dictQuery.getPageSize()), wrapper);
        return DictConvert.INSTANCE.convert(pageInfo);
    }

    /**
     * 添加
     */
    @Override
    public Boolean addDict(DictDTO dictDTO) {
        return dictMapper.insert(DictConvert.INSTANCE.convert(dictDTO)) > 0;
    }

    /**
     * 修改
     */
    @Override
    public Boolean updateDict(DictDTO dictDTO) {
        if (DictTypeEnum.SYSTEM.getType().equals(dictDTO.getSystemFlag())) {
            throw new BaseException("系统内置字典，不可修改!");
        }
        return dictMapper.updateById(DictConvert.INSTANCE.convert(dictDTO)) > 0;
    }

    @Override
    public DictDTO findById(Long id) {
        return DictConvert.INSTANCE.convert(dictMapper.selectById(id));
    }

    @Override
    public List<DictDTO> findAll() {
        return DictConvert.INSTANCE.convert(dictMapper.selectList(Wrappers.emptyWrapper()));
    }

    @Override
    public Boolean deleteDict(Long id) {
        SysDict sysDict = dictMapper.selectById(id);
        if (DictTypeEnum.SYSTEM.getType().equals(sysDict.getSystemFlag())) {
            throw new BaseException("系统内置字典，不可删除!");
        }
        // 删除所有字典项
        dictItemMapper.delete(Wrappers.<SysDictItem>lambdaQuery().eq(SysDictItem::getDictId, id));
        // 删除字典
        return dictMapper.deleteById(id) > 0;
    }

    /**
     * 根据字典类型获取字典下的所有字典项
     *
     * @param type 字典类型
     * @return 字典项
     */
    @Override
    public List<DictItemDTO> getDictItemsByType(String type) {
        List<SysDictItem> dictItems = dictItemMapper.selectList(Wrappers.<SysDictItem>lambdaQuery()
                .eq(SysDictItem::getType, type)
                .orderByAsc(SysDictItem::getSortOrder));
        return DictItemConvert.INSTANCE.convertList(dictItems);
    }
}